package com.itheima.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itheima.wemedia.pojo.WmUser;

import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.query;

@RestController
public class ApiController {

    @Autowired
    private WmUserService wmUserService;
    @Autowired
    private ApiService apiService;
    @Autowired
    private WmMaterialService wmMaterialService;
    @Autowired
    private WmNewsService wmNewsService;
    @PostMapping("/api/wmUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        return ResultVo.ok(wmUser);
    }
    @GetMapping("/api/wmUser/{appUserId}")
    public ResultVo<WmUser> getByAppUserId(@PathVariable(name = "appUserId")Integer appUserId){
        //这是用户id,
        LambdaQueryWrapper<WmUser> lqw = Wrappers.<WmUser>lambdaQuery();
        lqw.eq(null!=appUserId,WmUser::getApUserId,appUserId);
        WmUser wmUser = wmUserService.getOne(lqw);
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加素材
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo<WmMaterial> add(@RequestBody WmMaterial wmMaterial){
        wmMaterialService.save(wmMaterial);
        return ResultVo.ok();
    }

    /**
     * 查询所有的频道
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> channelList(){
        List<AdChannel> channelList = apiService.getChannelList();
        return ResultVo.ok(channelList);
    }

    /**
     * 根据文章id查询文章
     * @param newsId
     * @return
     */
    @GetMapping("/api/wmnews/{newsId}")
    ResultVo<WmNews> getWmNewsById(@PathVariable(name = "newsId")Integer newsId){
        WmNews wmNews = wmNewsService.getById(newsId);
        return ResultVo.ok(wmNews);
    }

    /**
     * 通过文章id更新
     * @param wmNews
     * @return
     */
    @PutMapping("/api/wmNews/update")
    ResultVo<WmNews> updateWmNews(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResultVo.ok();
    }

    /**
     * admin后台自媒体审核分页查询，经过网关调用
     * @param dto
     * @return
     */
    @PostMapping("/news/list_vo")
    public PageResultVo<WmNewsVo> findPageAdmin(@RequestBody WmNewsPageReqDto dto){
        PageResultVo<WmNewsVo> pageAdmin = wmNewsService.findPageAdmin(dto);
        return pageAdmin;
    }

    /**
     * admin微服务自媒体查看详情
     * @param wmNewsId
     * @return
     */
    @GetMapping("/news/one_vo/{wmNewsId}")
    public ResultVo<WmNewsVo> findByWmNewsId(@PathVariable(name = "wmNewsId") Integer wmNewsId){
        WmNews wmNews = wmNewsService.getById(wmNewsId);
        //根据userId查询作者
        WmUser wmUser = wmUserService.getById(wmNews.getUserId());
        return ResultVo.ok(WmNewsVo.build(wmNews,wmUser));
    }

    /**
     * 人工审核驳回
     * @param paramMap
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFail(@RequestBody Map<String, Object> paramMap){
        wmNewsService.authFail(paramMap);
        return ResultVo.ok();
    }

    /**
     * 人工审核通过
     * @param paramMap
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPass(@RequestBody Map<String, Object> paramMap){
        wmNewsService.authPass(((Integer) paramMap.get("id")));
        return ResultVo.ok();
    }
    /**
     * 查询审核通过的文章，状态为8。进行数据同步到文章微服务
     * @param
     * @return
     */
    @PostMapping("/api/wmNews/list4ArticleSync")
    ResultVo<List<WmNewsVo>> list4ArticleSync(@RequestBody WmNewsPageReqDto dto){
        List<WmNewsVo> wmNewsVos = wmNewsService.list4ArticleSync(dto);
        return ResultVo.ok(wmNewsVos);

    }







}
